﻿using GameBox.Framework;  // for GlobalLogger
using GameBox.Service.GiantLightServer;  // for IGiantLightClient

using System.Collections;

namespace Rpc
{
    public class Client : IGiantLightClient
    {
        public void OnDisconnect()
        {
            // 处理服务器连接丢失
            GlobalLogger.L("Disconnected!");
        }

        public bool PushRequest(uint id, string service, string method, byte[] content)
        {
            // 处理服务器推送过来的请求，返回true表示已经处理；反之则下一帧还会推送过来
            GlobalLogger.L("PushRequest: " + service + "." + method);
            if ("tanks.TanksPush" == service)
            {
                m_TanksPushService.Request(id, method, content);
                return true;
            }
            return true;
        }

        public delegate void ResponseHandler(byte[] content);

        public bool PushResponse(uint id, byte[] content)
        {
            // 处理服务器推送过来的响应，返回true表示已经处理；反之则下一帧还会推送过来
            GlobalLogger.L("PushResponse");
            if (!m_ResponseHandler.ContainsKey(id)) return true;
            ResponseHandler h = (ResponseHandler)m_ResponseHandler[id];
            m_ResponseHandler.Remove(id);
            h.Invoke(content);
            return true;
        }

        public void SetResponseHandler(uint rpcId, ResponseHandler responseHandler)
        {
            if (null == responseHandler) return;
            m_ResponseHandler.Add(rpcId, responseHandler);
            // Todo: timeout resonseHandler
        }

        private Hashtable m_ResponseHandler = new Hashtable();
        private Tanks.TanksPushService m_TanksPushService = new Tanks.TanksPushService();
    }  // class Client
}  // namespace Rpc
